package com.tensquare.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.experimental.var;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter {
    //1.过滤器的类型
    @Override
    public String filterType() {
        return "pre";
    }
    //2.过滤器的执行顺序
    @Override
    public int filterOrder() {
        return 0;
    }
    //3.过滤器是否启用
    @Override
    public boolean shouldFilter() {
        return true;
    }
    //4.执行具体的业务
    @Override
    public Object run() throws ZuulException {
        //1.得到当前请求的上下文对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        //2.得到请求对象
        HttpServletRequest request = currentContext.getRequest();
        //3.得到请求头
        String header = request.getHeader("Authorization");
        //4.判断是否为空
        if(header != null){
            currentContext.addZuulRequestHeader("Authorization",header);
        }
        return null;
    }
}
